<?php

namespace App\Services\Infra;

use App\Models\AdminInfo;
use App\Models\RoleUser;
use App\Models\UserInfo;
use App\Models\User;
use App\Models\UserDevice;
use App\Exceptions\UserException;
use Cache;
use Carbon\Carbon;
use DB;
use Exception;
use Illuminate\Database\QueryException;
use Sentinel;

class UserManager
{
    /**
     * 增加用户信息
     * @param array $data [用户信息数据]
     */
    public function addUserInfo($data)
    {
        try{
            UserInfo::create($data);
        }catch(Exception $e){
            throw new UserException('userInfo.add');
        }
    }

    /**
     * [updateUserInfo description]
     * @param  integer  $userId 
     * @param  array  $data   
     * @return          
     */
    public function updateUserInfo($userId, $data)
    {
        try{
            $userInfo = UserInfo::where('user_id', $userId)->first();
            if(is_null($userInfo)){
                throw new UserException('userInfo.notFound');
            }
            $userInfo->update($data);
        }catch(Exception $e){
            throw new UserException('userInfo.update');
        }
    }

    /**
     * 获取用户
     * @return 
     */
    public function getUser()
    {
    	$user = Sentinel::getUser();
        if(is_null($user))
        {
            throw new UserException('notFound');
        }
        
	    return $user;
    }

    /**
     * [getUserInfoByUsername description]
     * @param  string $username 
     * @return            
     */
    public function getUserInfoBuilderByUsername($username)
    {
        return UserInfo::where('open_id', $username);
    }

    public function getUserInfoBuilderByColumn($column, $condiiton)
    {
        return UserInfo::where($column, $condiiton);
    }

    /**
     * 
     */
    public function addAdminInfo($data)
    {
        AdminInfo::create($data);
    }

    /**
     * 
     * @return 
     */
    public function getAdminInfoBuilder()
    {
        return AdminInfo::latest();
    }

    /**
     * [getRoleUserByQuery description]
     * @param  string $column   
     * @param  string $condition
     * @return            
     */
    public function getRoleUserByQuery($column, $condition)
    {
        $roleUser = RoleUser::where($column, $condition)->first();
        if(is_null($roleUser)){
            throw new UserException('role.notFound');
        }
        return $roleUser;
    }

    /**
     * [getUserByQuery description]
     * @param  string $column   
     * @param  string $condition
     * @return            
     */
    public function getUserByQuery($column, $condition)
    {
        $user = User::where($column, $condition)->first();
        if(is_null($user)){
            throw new UserException('user.notFound');
        }
        return $user;
    }

    /**
     * 
     * @param  arrray $deviceIds 
     * @return             
     */
    public function findUserIdByDeviceIds($deviceIds)
    {
        return UserDevice::whereIn('device_id', $deviceIds)->get();
    }

    /**
     * 
     * @param  string $condition 
     * @return             
     */
    public function findUserInfoBuilederByNameOrPhone($condition)
    {
        return UserInfo::where(function($query) use ($condition) {
            $query->where('name', 'like', '%'.$condition.'%')->orWhere('phone', 'like', '%'.$condition.'%');
        });
    }

}
